﻿
namespace Patterns4Net.Tests.Core.CecilExtensions.CILPatternsMatching
{
    using Mono.Cecil.Cil;
    using NUnit.Framework;
    using Patterns4Net.Core.CecilExtensions.CILPatternsMatching;

    public class PredicateInstructionMatcherTest
    {
        [Test]
        [TestCase(true)]
        [TestCase(false)]
        public void ReturnsPredicateResult(bool result)
        {
            var matcher = new PredicateInstructionMatcher(i => result);
            Instruction next;
            var current = Instruction.Create(OpCodes.Ldc_I4_3);
            Assert.AreEqual(result, matcher.Matches(current, out next));
        }

        [Test]
        public void SetsDefaultNextInstruction()
        {
            var matcher = new PredicateInstructionMatcher(i => true);
            Instruction next;
            var current = Instruction.Create(OpCodes.Ldc_I4_0);
            current.Next = Instruction.Create(OpCodes.Nop);
            matcher.Matches(current, out next);

            Assert.AreEqual(current.Next, next);
        }

        [Test]
        public void SetsNextInstructionByPredicate()
        {
            var matcher = new PredicateInstructionMatcher(i => true, i => Instruction.Create(OpCodes.Ldc_I4_2));
            Instruction next;
            var current = Instruction.Create(OpCodes.Ldc_I4_0);
            current.Next = Instruction.Create(OpCodes.Nop);
            matcher.Matches(current, out next);

            Assert.AreEqual(OpCodes.Ldc_I4_2, next.OpCode);
        }
    }
}
